﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace TinyCinema.Data
{
    public class Genre : Execution
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public ObservableCollection<Movie> Movies { get; set; }
        public static ObservableCollection<Genre> lstGenre { get; set; }
        int _Index { get; set; }
        public int Index
        {
            get { return _Index; }
            set
            {
                ID = lstGenre[value].ID;
                Name = lstGenre[value].Name;
                _Index = value;
            }
        }
        public Genre() {
            lstGenre = new ObservableCollection<Genre>();
            lstGenre = getAllGenre();
            Index = 0;
        }
        public Genre(string _ID)
        {
            PutTableIntoDataSet(Tables.Genre);
            var tbl = ds.Tables[Tables.Genre];
            var Row = tbl.Select(GenreTable.ID + "='" + _ID + "'")[0];
            ID = Row[GenreTable.ID] as String;
            Name = Row[GenreTable.Name] as String;

            if (lstGenre == null)
            {
                lstGenre = new ObservableCollection<Genre>();
                lstGenre = getAllGenre();
            }
            for (int i = 0; i < lstGenre.Count; ++i)
                if (ID == lstGenre[i].ID) Index = i;

        }
        static ObservableCollection<Genre> getAllGenre()
        {
            var lst = new ObservableCollection<Genre>();
            PutTableIntoDataSet(Tables.Genre);
            var tbl = ds.Tables[Tables.Genre];
            var Rows = tbl.Select();
            lst = new ObservableCollection<Genre>();
            foreach (var row in Rows)
                lst.Add(new Genre(row[GenreTable.ID] as String));
            return lst;
        }
    }
}
